/*
 * Copyright (c) 2018 Hitevision
 */

package tw.com.hitevision.whiteboard.android.state;

import android.content.ContentResolver;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.provider.OpenableColumns;
import android.util.Log;

import androidx.annotation.ColorRes;
import androidx.annotation.DrawableRes;
import tw.com.hitevision.whiteboard.android.R;

public class PageProperty {
    private Color color;
    private Pattern pattern;
    private Drawable customBackground;
    private boolean isLightBackground;

    private String customBackgroundName;

    public PageProperty(Color color, Pattern pattern) {
        this.color = color;
        this.pattern = pattern;
        isLightBackground = color.equals(Color.LIGHT_YELLOW) || color.equals(Color.WHITE) || color.equals(Color.YELLOW);
        customBackground = null;
    }

    public Color getColor() {
        return color;
    }

    public int getColorInt(){
        if(color == null)
            return 0;
        return color.ordinal();
    }

    public int getPatternInt(){
        if(pattern == null)
            return 0;
        return pattern.ordinal();}

    public void setColor(Color color) {
        this.color = color;
    }

    public Pattern getPattern() {
        return pattern;
    }

    public void setPattern(Pattern pattern) {
        this.pattern = pattern;

        if (pattern != Pattern.IMPORT) {
            customBackground = null;
        }
    }

    public void setCustomBackground(Resources res, Bitmap background) {
        if (background != null) {
            Log.d("PageProp", "setCustomBackground: backgroundset");
            customBackground = new BitmapDrawable(res, background);
            pattern = Pattern.IMPORT;
        } else {
            Log.d("PageProp", "setCustomBackground: background null");
        }
    }

    void setCustomBackground(Drawable drawable) {
        if (drawable != null) {
            customBackground = drawable;
            pattern = Pattern.IMPORT;
        } else {
            // if it's import and null background, set to blank
            if(pattern == Pattern.IMPORT)
                pattern = Pattern.BLANK;
        }
    }

    @ColorRes
    int getColorRes() {
        if (color == null) {
            return 0;
        }

        switch (color) {
            case WHITE:
                return R.color.page_white;
            case BLACK:
                return R.color.page_black;
            case GREEN:
                return R.color.page_green;
            case LIGHT_GREEN:
                return R.color.page_light_green;
            case YELLOW:
                return R.color.page_yellow;
            case LIGHT_YELLOW:
                return R.color.page_light_yellow;
            case BLUE:
                return R.color.page_blue;
            case TRANSPARENT:
                return R.color.transparent;
            default:
                return 0;
        }
    }

    public void setColor(int index) {
        setColor(Color.values()[index]);
    }

    public void setPattern(int index) {
        setPattern(Pattern.values()[index]);
    }


    @DrawableRes
    int getPatternRes() {
        if (pattern == null) {
            return 0;
        }

        switch (pattern) {
            case BLANK:
                return 0;
            case LINES:
                return R.drawable.bg_img_lines;
            case GRID:
                return R.drawable.bg_img_grid;
            case NOTEBOOK:
                return R.drawable.bg_img_notebook;
            case ALPHABET_5_SET:
                return isLightBackground?R.drawable.bg_img_alphabet_5_sets:R.drawable.ic_img_alphabet_5_sets_light;
            case ALPHABET_9_SET:
                return isLightBackground?R.drawable.bg_img_alphabet_9_sets:R.drawable.ic_img_alphabet_9_sets_light;
            case CHECK_LIST:
                return R.drawable.bg_img_checklist;
            case SWOT:
                return R.drawable.bg_img_swot;
            case MUSIC_TREBLE:
                return isLightBackground?R.drawable.bg_img_musicscore_1:R.drawable.ic_img_musicscore_1_light;
            case MUSIC_BASS:
                return isLightBackground?R.drawable.bg_img_musicscore_2:R.drawable.ic_img_musicscore_2_light;
            case BASEBALL:
                return R.drawable.bg_img_baseball;
            case BASKETBALL:
                return R.drawable.bg_img_basketball;
            case SOCCER:
                return R.drawable.bg_img_soccor;
            case FOOTBALL:
                return R.drawable.bg_img_football;
            case IMPORT:
                return -1;
            default:
                return 0;
        }
    }

    boolean isCustomBackgroundSet() {
        return customBackground != null;
    }

    public void generateCustomBackgroundName(String fileName) {
        customBackgroundName = "background_" + fileName + ".png";
    }

    // hitevision debug
    public void generateCustomBackgroundName(Uri contentUri, ContentResolver contentResolver) {
//        if (contentUri != null) {
//            List<String> segments = contentUri.getPathSegments();
//            int size = segments.size();
//            if (size > 0) {
//                String fileName;
//
//                fileName = size == 1 ? segments.get(size - 1) : segments.get(size - 2) + "_" + segments.get(size - 1);
//
//                fileName = fileName.replace(":", "_");
//
//                customBackgroundName = "background_" + fileName + ".png";
//            }
//        }
        if (contentUri == null) {
            return;
        }

        String fileName = null;

        Cursor cursor = contentResolver.query(contentUri, null, null, null, null);
        if (contentUri.getScheme() != null && contentUri.getScheme().equals("content")) {
            if (cursor != null) {
                int nameIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
                cursor.moveToFirst();
                fileName = cursor.getString(nameIndex);
                cursor.close();
            }
        }

        if (fileName == null) {
            String path = contentUri.getPath();
            if (path != null && !path.isEmpty()) {
                int slashIndex = path.lastIndexOf("/");
                if (slashIndex != -1) {
                    fileName = path.substring(slashIndex + 1);
                }
            }
        }

        if (fileName != null && !fileName.isEmpty()) {
            if (fileName.contains("/")) {
                fileName.replace("/", "_");
            }
            customBackgroundName = "background_" + fileName;
            Log.d("TEST", "Generate file name : " + customBackgroundName);
        }
    }

    public String getCustomBackgroundName() {
        return customBackgroundName;
    }

    public Drawable getCustomBackground() {
        return customBackground;
    }

    boolean isDark() {
        return color == Color.BLACK || color == Color.GREEN || color == Color.BLUE;
    }

    public enum Color {
        WHITE, BLACK, GREEN, LIGHT_GREEN, YELLOW, LIGHT_YELLOW, BLUE, TRANSPARENT
    }

    public enum Pattern {
        BLANK, LINES, GRID, NOTEBOOK, ALPHABET_5_SET, ALPHABET_9_SET, CHECK_LIST, SWOT, MUSIC_TREBLE, MUSIC_BASS,
        BASEBALL, BASKETBALL, SOCCER, FOOTBALL, IMPORT
    }
}
